package com.webcamtracker.color.segmentation;

import com.webcamtracker.move.tracking.AplicationListener;
import com.webcamtracker.move.tracking.ListenersLoader;
import com.webcamtracker.move.tracking.TrackingEvent;
import com.webcamtracker.move.tracking.TrackingEventDispacher;
import com.webcamtracker.move.tracking.TrackingEventListener;
import com.webcamtracker.move.tracking.TrackingEventType;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventListenerManger implements TrackingEventDispacher {
    private static EventListenerManger ourInstance = new EventListenerManger();
    private List<AplicationListener> listeners;
    private Map<TrackingEventType, List<TrackingEventListener>> eventListeners;

    public static EventListenerManger getInstance() {
        return ourInstance;
    }

    private EventListenerManger() {
        this.listeners = ListenersLoader.getListeners();
        this.eventListeners = new HashMap<TrackingEventType, List<TrackingEventListener>>();
    }

    public void registerNewObjectToTrack(String name) {
        for (AplicationListener listener : listeners) {
            listener.subscribeToTracking(this, name);
        }
    }

    public void addTrackingEventListener(TrackingEventListener eventListener, TrackingEventType... trackingEventTypes) {
        for (TrackingEventType trackingType : trackingEventTypes) {
            if (getListenersForType(trackingType) == null) {
                this.eventListeners.put(trackingType, new ArrayList<TrackingEventListener>());
            }

            getListenersForType(trackingType).add(eventListener);
        }
    }

    public void objectFound(TrackingObject object, String name, float zoomFactor, BufferedImage image) {
        final List<TrackingEventListener> forType = getListenersForType(TrackingEventType.ALL);
        final Point dimension = new Point(object.getWidth(), object.getHeight());
        final Point point = new Point(object.getX(), object.getY());
        final TrackingEvent event = new TrackingEvent(object, point, dimension, TrackingEventType.ALL, name,zoomFactor, image);
        for (TrackingEventListener trackingEventListener : forType) {
            trackingEventListener.trackingEventDispatched(event);
        }

    }

    protected List<TrackingEventListener> getListenersForType(TrackingEventType trackingType) {
        List<TrackingEventListener> listeners = this.eventListeners.get(trackingType);

        if (listeners == null) {
            final ArrayList<TrackingEventListener> list = new ArrayList<TrackingEventListener>();
            this.eventListeners.put(trackingType, list);
            return list;
        }

        return listeners;
    }

    public boolean removeTrackingEventListener(TrackingEventListener eventListener, TrackingEventType... trackingEventTypes) {
        boolean result = false;

        for (TrackingEventType trackingType : trackingEventTypes) {
            final List<TrackingEventListener> list = getListenersForType(trackingType);
            result |= list.remove(eventListener);
        }

        return result;
    }
}
